# -*- coding: utf-8 -*-
from __future__ import unicode_literals
from .models import User, Collect
from articleModel.models import Article
from toolFunctions import response, validator, checkLogin
from django.core.exceptions import ObjectDoesNotExist
from django.forms.models import model_to_dict
from django.core.paginator import Paginator, EmptyPage, PageNotAnInteger
import json


#   用户注册
def signup(request):
    password = request.POST.get('password')
    email = request.POST.get('email')
    head_image = '/static/image/default_head.png'
    username = request.POST.get('username', email)

    if request.method != 'POST':
        return response.fail('ERROR_RES_METHOD')

    if response.request_is_empty(email):
        return response.fail('', '邮箱不能为空！')

    if not validator.is_email(email):
        return response.fail('', '邮箱格式不正确！')

    if response.request_is_empty(password):
        return response.fail('', '密码不能为空！')

    #   邮箱唯一性校验
    try:
        user = User.objects.get(email=email)
        return response.fail('', '该邮箱已注册过啦！')
    except ObjectDoesNotExist:
        pass

    #   创建用户
    user = User(password=password, username=username, email=email, head_img=head_image)
    user.save()

    return response.success('用户创建成功')


#   用户登录
def signin(request):
    json_result = json.loads(request.body)
    password = json_result['password']
    email = json_result['email']

    if request.method != 'POST':
        return response.fail('ERROR_RES_METHOD')

    if response.request_is_empty(email):
        return response.fail('', '邮箱不能为空！')

    if not validator.is_email(email):
        return response.fail('', '邮箱格式不正确！')

    if response.request_is_empty(password):
        return response.fail('', '密码不能为空！')

    try:
        user = User.objects.get(email=email, password=password)
        request.session['is_login'] = '1'
        request.session['user_id'] = user.id

        return response.success(model_to_dict(user))
    except ObjectDoesNotExist:
        return response.fail('', '该邮箱未注册或密码错误')

    return response.fail()


#   退出登录
@checkLogin.check_login
def signout(request):
    try:
        request.session['is_login'] = ''
        del request.session['user_id']
    except:
        pass

    return response.success()


#   获取用户信息
@checkLogin.check_login
def getuserinfo(request):
    user_id = request.session.get('user_id')
    user = User.objects.get(id=user_id)

    if user is None:
        return response.fail('', '未找到登录用户')

    return response.success(model_to_dict(user))



#   收藏列表
@checkLogin.check_login
def collects(request):
    pageNo = request.GET.get('pageNo', 1)
    pageSize = request.GET.get('pageSize', 15)
    user_id = request.session.get('user_id')

    res = {}
    collects = Collect.objects.filter(user_id=user_id).values()
    article_ids = []
    for collect in collects:
        article_ids.append(collect['article_id'])

    articles = Article.objects.filter(status=1, id__in=article_ids)
    paginator = Paginator(articles, pageSize)
    res['total'] = paginator.count

    try:
        list = paginator.page(pageNo)
    except PageNotAnInteger:
        list = paginator.page(1)
    except EmptyPage:
        list = []

    arr_list = []
    for item in list:
        dict_item = model_to_dict(item)
        arr_list.append(dict_item)

    res['items'] = arr_list

    return response.success(res)